//
//  newestViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "newestViewController.h"
#import "HeaderModel.h"
#import "newestOtherCell.h"
#import "newestCell.h"
#import "newestModel.h"
#import "BaseModel.h"
#import "SDCycleScrollView.h"
#import "newsestDetailWebViewController.h"
@interface newestViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *newestTableV;
@property(nonatomic, retain)NSMutableArray *newestArray;
@property(nonatomic, retain)NSMutableArray *headerArr;
@property(nonatomic, retain)NSMutableArray *imageArr;
@property(nonatomic, retain)MBProgressHUD *HUB;
@property(nonatomic, assign)NSInteger nextPage;//记录下一页
@property(nonatomic, assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉
@property(nonatomic, strong)NSMutableArray *images;
@property(nonatomic, strong)UIScrollView *scroll;
@end

@implementation newestViewController

-(void)dealloc{
    [_imageArr release];
    [_headerArr release];
    [_HUB release];
    [_newestTableV release];
    [_newestArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUB.mode = MBProgressHUDModeIndeterminate;//不同类型
    [self.HUB show:YES];
    self.HUB.labelText = @"loading...";
    self.isUpLoading = NO;//进入界面下拉
    // Do any additional setup after loading the view.
    self.newestArray = [NSMutableArray array];
    self.headerArr = [NSMutableArray array];
    self.imageArr = [NSMutableArray array];
    
    self.newestTableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.newestTableV.backgroundColor = [UIColor clearColor];
    self.newestTableV.delegate = self;
    self.newestTableV.dataSource = self;
    [self.view addSubview:self.newestTableV];
    [self.newestTableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    self.newestTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_newestTableV release];
    
    
    [self.newestTableV registerClass:[newestCell class] forCellReuseIdentifier:@"newest"];
    [self.newestTableV registerClass:[newestOtherCell class] forCellReuseIdentifier:@"newestOther"];
    
    
    //上下拉图片数组
    self.images = [NSMutableArray array];
    for (int i = 1; i < 4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_0%d.png",i]];
        [self.images addObject:image];
    }
    [self addHeader];
    [self addFooter];
    [self.newestTableV.mj_header beginRefreshing];
    
    
    //头视图
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width, 120);
    [self.view addSubview:self.scroll];
    self.newestTableV.tableHeaderView = self.scroll;
    [self getHeader];


}

-(void)getHeader{
    [ZYNetworkHandle GetDataWithURL:@"http://app.api.autohome.com.cn/autov4.2.5/news/newslist-a2-pm1-v4.2.5-c0-nt0-p1-s30-l0.html" dic:nil success:^(id responseObject) {
        NSDictionary *result = [responseObject objectForKey:@"result"];
        NSMutableArray *foctArr = [result objectForKey:@"focusimg"];
        for (NSDictionary *dic in foctArr) {
            HeaderModel *model = [[HeaderModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.headerArr addObject:model];
            [self.imageArr addObject:model.imgurl];
            //NSLog(@"%@",model.imgurl);
            [model release];
            SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 5, self.view.bounds.size.width, 180) delegate:nil placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cycleScrollView.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"];
            cycleScrollView.pageDotImage = [UIImage imageNamed:@"pageControlDot"];
            cycleScrollView.imageURLStringsGroup = self.imageArr;
            [self.scroll addSubview:cycleScrollView];
            
        }
        
        [self.newestTableV reloadData];
        [self.HUB hide:YES];//菊花条
    } filed:^(NSError *error) {
        
        
    }];



}
//下拉刷新
-(void)addHeader{
    __block newestViewController *newVC = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
       // newVC.nextPage = 1;//记录页码
        newVC.isUpLoading = NO;//标记下拉操作
        [newVC getDataWith:@"http://app.api.autohome.com.cn/autov4.2.5/news/newslist-a2-pm1-v4.2.5-c0-nt0-p1-s30-l0.html"];//每次下拉重新请求数据
    }];
    [header setImages:self.images forState:MJRefreshStateIdle];
    [header setImages:self.images forState:MJRefreshStatePulling];
    [header setImages:self.images forState:MJRefreshStateRefreshing];
    self.newestTableV.mj_header = header;
}

//上拉加载
-(void)addFooter{
    __block newestViewController *newVC = self;
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        newVC.nextPage ++;//上拉一次自增一次
        newVC.isUpLoading = YES;//标记为上拉操作
        newestModel *model = [self.newestArray lastObject];
        NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.2.5/news/newslist-a2-pm1-v4.2.5-c0-nt0-p%d-s30-l%@.html",newVC.nextPage + 1,model.lasttime];
        [newVC getDataWith:str];//每次上拉请求数据
    }];
    [footer setImages:self.images forState:MJRefreshStateRefreshing];
    self.newestTableV.mj_footer = footer;
}


-(void)getDataWith:(NSString *)str{
    [ZYNetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        NSDictionary *result = [responseObject objectForKey:@"result"];
        NSMutableArray *listArr = [result objectForKey:@"newslist"];
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.newestArray  removeAllObjects];
        }
        for (NSDictionary *dic in listArr) {
            newestModel *model = [[newestModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.newestArray addObject:model];
           // NSLog(@"%ld",(long)model.myId);
            [model release];
        }
        [self.newestTableV.mj_header endRefreshing];
        [self.newestTableV.mj_footer endRefreshing];
        [self.newestTableV reloadData];
        [self.HUB hide:YES];//菊花条
    } filed:^(NSError *error) {
        
        
    }];

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    newsestDetailWebViewController *VC = [[newsestDetailWebViewController alloc] init];
    newestModel *model = [self.newestArray objectAtIndex:indexPath.row];
    VC.ID = [NSString stringWithFormat:@"%ld",(long)model.myId];
    VC.type = [NSString stringWithFormat:@"%ld",(long)model.mediatype];
    [self.navigationController pushViewController:VC animated:YES];
}








-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    newestModel *model = [self.newestArray objectAtIndex:indexPath.row];
    if (model.mediatype == 6) {
        return 165;
    }else{
        return 130;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newestArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    newestModel *model = [self.newestArray objectAtIndex:indexPath.row];
    if (model.mediatype == 6) {
        newestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newestOther" forIndexPath:indexPath];
        cell.newsestmodel = model;
        return cell;
    }else{
    newestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newest" forIndexPath:indexPath];
    cell.newsestmodel = model;
    return cell;
    }
}



@end
